    //
//  MyNavigationController.m
//  SmartRoadShow
//
//  Created by simon on 10-9-17.
//  Copyright 2010 simonwang142232@gmail.com. All rights reserved.
//

#import "MyNavigationController.h"
#import "SmartRoadShowAppDelegate.h"
#import "DefaultViewController.h"
#import "MyTabBarController.h"
#import "MyTabBar.h"
#import <QuartzCore/QuartzCore.h>

@interface MyNavigationController()
-(void) backAction;
@end


@implementation MyNavigationController
@synthesize navigationBar, topViewController, title;


- (id)initWithRootViewController:(UIViewController *)rootViewControllerPara{
	if(self = [super init]){
		
		//navigation bar
		navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, kNavigationBarHeight)];
		navigationBar.layer.contents = (id)[UIImage imageNamed:@"navigationBar_bg.png"].CGImage;		
		[self.view addSubview:navigationBar];
		
		
		//  *这一段代码有何用处？？？
		UIControl *control = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, 90, kNavigationBarHeight)];
		[control addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
		[navigationBar addSubview:control];
		[control release];
		
		
		//back button
		UIButton* backButton = [UIButton buttonWithType:UIButtonTypeCustom];
		backButton.frame = CGRectMake(10, 8, 56, 28);
		[backButton setBackgroundImage:[UIImage imageNamed:@"backButton.png"] forState:UIControlStateNormal];
		[backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
		[navigationBar addSubview:backButton];
		
		
		//contentView and rootView
		contentView = [[UIView alloc] initWithFrame:CGRectMake(0, kNavigationBarHeight, 320, kContentViewHeight)];
		[self.view addSubview:contentView];
		rootViewControllerPara.view.frame = CGRectMake(0, 0, 320, kContentViewHeight);
		[contentView addSubview:rootViewControllerPara.view];

		
		//stack
		stack = [[NSMutableArray alloc] init];
		[stack addObject:rootViewControllerPara];
		
		//
		titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 40)];
		titleLabel.center = CGPointMake(160, kNavigationBarHeight/2);
		titleLabel.textAlignment = UITextAlignmentCenter;
		titleLabel.backgroundColor = [UIColor clearColor];
		titleLabel.font = kNavigationBarFont;
		[navigationBar addSubview:titleLabel];
		[titleLabel release];
		
		hasSoloController = YES;
	}
	return self;
}


-(void) backAction{
	
	NSLog(@"statc = %d",[stack count]);
	
	if([stack count] == 1){
		[self dismissModalViewControllerAnimated:YES];

		DefaultViewController* defaultVC = (DefaultViewController*)(((SmartRoadShowAppDelegate*)[UIApplication sharedApplication].delegate).tabBarController.defaultController);		
		[tabBarController setContentView:defaultVC.view];
		[tabBarController.tabBar setAllButtonUnselected];	
	}
	else {
		if ([self.title isEqualToString:@"城市宣言"]) {
			self.title = @"都市新主张";
			for (UIView *subview in self.navigationBar.subviews){
				if (subview.tag == 803) {
					[subview setHidden:NO];
				}
			}
		}
		[self popViewControllerAnimated:YES];
	}
}



-(void)setTitle:(NSString *)titlePara{
	title = titlePara;
	[titleLabel setText:titlePara];
	

}


- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
	
	[stack addObject:viewController];
	viewController.view.frame = contentView.frame;
	[self.view addSubview:viewController.view];
}


- (UIViewController *)popViewControllerAnimated:(BOOL)animated{
	
	if(hasSoloController == NO){
		hasSoloController = YES;
	}
	
	UIViewController* vc = (UIViewController*)[stack lastObject];
	[vc.view removeFromSuperview];
	[stack removeLastObject];

	return vc;
}

-(void) pushSoloViewController:(UIViewController*)viewController{
	if(hasSoloController == YES){
		[stack addObject:viewController];
		viewController.view.frame = contentView.frame;
		[self.view addSubview:viewController.view];
		hasSoloController = NO;
	}
}


-(MyViewController*)topViewController{
	
	return [stack lastObject];
}

- (void) bringViewToTop:(UIView*)aView{
	
}


- (void)dealloc {
	[stack release];
	[navigationBar release];
	[contentView release];
	[topViewController release];
    [super dealloc];
}


@end
